# coding=utf-8
"""
Created on 2018年8月9日
"""
import unittest
import time
import json
import hashlib
import requests


class InternalTestCases(unittest.TestCase):
    
    # run before each test method
    def setUp(self):
        """
        """
        # LocalHost
        self.host = "http://127.0.0.1"

        # DEV
        # self.host = "http://54.191.225.93"
        # self.host = "http://dev.alive-story.com"
        # self.host = "https://devs.alive-story.com"

        # PROD
        # self.host = "http://34.216.225.230"  # prod-oam-server
        # self.host = "http://prod.alive-story.com"
        self.host = "https://prods.alive-story.com"

        self.port = 20000
        self.uri = '/internal/v1/'
        self.url = '%s:%s%s'% (self.host, self.port, self.uri)
        self.app_version = '6.6'
        self.secretKey = 'alive_2018'
        pass

    # run after each test method
    def tearDown(self):
        pass

    def test_20000(self):
        """
        test
        """

        cmdid = 20000

        headers = {
            "AppVersion": self.app_version,
            "Timestamp": str(int(time.time() * 1000)),
            "SignType": 'md5',
            "RequestSign": '',
            'cmdId': str(cmdid)
        }

        body = {
            "common": {
                'userId': 'xxxx',
                'cmdId': cmdid,
                'release': 1,
                'platform': 2
            },
            "params": {
                'param1': '111'
            }
        }

        body = json.dumps(body)
        sign_str = '''AppVersion=%s&Body=%s&SignType=md5&Timestamp=%s&secretKey=%s''' \
                   % (self.app_version, body, headers['Timestamp'], self.secretKey)

        headers['RequestSign'] = hashlib.md5(sign_str.encode()).hexdigest().upper()
        start = time.time()
        res = requests.post(self.url, data=body, headers=headers)
        end = time.time()

        try:
            print(json.dumps(json.loads(res), indent=4))
        except:
            print(res.text)

        print('...耗时%sms...' % ((end - start) * 1000))
    

if __name__ == '__main__':
    suite = unittest.TestSuite()
    suite.addTest(InternalTestCases('test_23001'))
    unittest.TextTestRunner(verbosity=2).run(suite)
